//
//  LLMallHeaderView.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/4.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLMallHeaderView.h"

@interface LLMallHeaderView ()<SDCycleScrollViewDelegate>
@property (strong, nonatomic) SDCycleScrollView *cycleScrollView;
@end

@implementation LLMallHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero imageNamesGroup:@[@"placeholder"]];
    _cycleScrollView.frame = CGRectMake(0, 0, LLScreenWidth, LLScreenWidth*0.2);
    _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    _cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    _cycleScrollView.clipsToBounds = YES;
    _cycleScrollView.delegate = self;
    _cycleScrollView.pageDotColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    //    _cycleScrollView.currentPageDotColor = FWSColor(254, 85, 46);
    //    _cycleScrollView.pageControlBottomOffset = -5;
    [self addSubview:_cycleScrollView];
    
    CGFloat btnH = 80;
    CGFloat btnY = _cycleScrollView.bottom + 10;
    CGFloat btnW = 80;
    NSArray *titles = @[@"热销课程",@"0.01元拼团",@"组队砍价",@"积分换购"];
    NSArray *imgs = @[@"热销课程",@"0.01元拼团",@"组队砍价",@"积分换购"];
    for (NSInteger i = 0; i < 4; i++) {
        LLButton *btn = [LLInitBaseViewTool buttonWithTitle:titles[i] image:imgs[i] titleColor:LLColorRGB(46) font:11];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        CGFloat margin = (self.width-btnW*imgs.count)/(imgs.count+1);
        btn.frame = CGRectMake(margin+i*(margin+btnW), btnY, btnW, btnH);
        CGFloat spacing = 10;
        [self verticalCenterImageAndTitle:spacing btn:btn];
        [self verticalCenterImageAndTitleWithBtn:btn];
    }
}

- (void)verticalCenterImageAndTitle:(CGFloat)spacing btn:(UIButton *)btn{
    CGSize imageSize = btn.imageView.frame.size;
    CGSize titleSize = btn.titleLabel.frame.size;
    btn.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing/2), 0.0);
    titleSize = btn.titleLabel.frame.size;
    btn.imageEdgeInsets = UIEdgeInsetsMake(- (titleSize.height + spacing/2), 0.0, 0.0, - titleSize.width);
}

- (void)verticalCenterImageAndTitleWithBtn:(UIButton *)btn{
//    const int DEFAULT_SPACING = 10.0f;
    [self verticalCenterImageAndTitle:10 btn:btn];
}

- (void)btnClick:(LLButton *)btn {
    if ([self.delegate respondsToSelector:@selector(mallHeaderViewDidClickWithButton:)]) {
        [self.delegate mallHeaderViewDidClickWithButton:btn];
    }
}

@end
